# 类型检查
puts 42.class        # Integer
puts 3.14.class      # Float
puts (2/3r).class    # Rational (Ruby 2.4+)
puts (1+2i).class    # Complex

puts "==========================================="

# 基本运算

# 四舍五入
puts 3.14159.round(2)  # 3.14

# 向上取整
puts 3.2.ceil    # 4

# 向下取整
puts 3.9.floor   # 3

# 绝对值
puts -10.abs     # 10

# 取商和余数
puts 10.divmod(3) # [3, 1]

puts "==========================================="

# 数学运算

# 幂运算
puts 2 ** 10      # 1024
puts 3.pow(3)     # 27

# 平方根
puts Math.sqrt(16) # 4.0

# 对数
puts Math.log(100, 10) # 2.0

# 三角函数
puts Math.sin(Math::PI/2) # 1.0

puts "==========================================="

# 数值类型转换

# 转整数
puts "123".to_i     # 123
puts 3.14.to_i      # 3

# 转浮点数
puts "3.14".to_f    # 3.14
puts 5.to_f         # 5.0

# 转有理数
puts 1.5.to_r       # (3/2)

# 转复数
puts 2.to_c         # (2+0i)

# 转字符串
puts 123.to_s       # "123"

puts "==========================================="

# 字符串创建

# 单引号 (不转义)
str1 = 'Hello\nWorld'  # 字面显示\n

# 双引号 (支持转义和表达式嵌入)
str2 = "Hello\nWorld #{Time.now.year}"

# %q (相当于单引号)
str3 = %q(Ruby's syntax)

# %Q (相当于双引号)
str4 = %Q(Current year: #{Time.now.year})

# Here Document
str5 = <<~EOF
  Multiline
  String
EOF

puts str1
# 输出: Hello\nWorld

puts str2
# 输出：Hello
# World 2025

puts str3
# 输出：Ruby's syntax

puts str4
# 输出：Current year: 2025

puts str5
# 输出：
# Multiline
# String

puts "==========================================="

s = "Hello Ruby"

# 长度
puts s.length     # 10
puts s.size       # 10 (同length)

# 包含检查
puts s.include?("Ruby")  # true

# 开头/结尾检查
puts s.start_with?("He") # true
puts s.end_with?("by")   # true

# 索引位置
puts s.index("Ruby")     # 6

# 替换
puts "hello".gsub(/l/, 'L')  # "heLLo"

# 删除
puts " hello ".strip    # "hello" (去首尾空格)
puts "abc123".delete("0-9") # "abc"

# 反转
puts "ruby".reverse    # "ybur"

# 大小写转换
puts "Ruby".downcase   # "ruby"
puts "Ruby".upcase     # "RUBY"
puts "hello world".capitalize # "Hello world"

puts "==========================================="

# 分割字符串
puts "a,b,c".split(",")  # ["a", "b", "c"]

# 连接数组
puts ["a", "b", "c"].join("-") # "a-b-c"

# 每行处理
"line1\nline2".each_line { |line| puts line }

puts "==========================================="

# 转整数
puts "123".to_i       # 123
puts "123abc".to_i    # 123 (忽略非数字部分)

# 转浮点数
puts "3.14".to_f      # 3.14

# 转数组
puts "hello".chars    # ["h", "e", "l", "l", "o"]

# 转符号
puts "hello".to_sym    # :hello

# 编码转换
str = "こんにちは"
puts str.encode("UTF-8")

puts "==========================================="

# 数值转字符串

# 基本转换
puts 123.to_s        # "123"

# 格式化输出
puts "Value: %05d" % 123   # "Value: 00123"
puts "%.2f" % 3.14159     # "3.14"

# 高级格式化 (sprintf)
puts sprintf("%+10.2f", 3.14159) # "     +3.14"

puts "==========================================="

# 字符串转数值

# 安全转换
Integer("123") rescue nil  # 123
Float("3.14") rescue nil   # 3.14

# 带基数转换
puts "1010".to_i(2)  # 10 (二进制转十进制)

# 严格转换
# Integer("123abc")     # 抛出ArgumentError

puts "==========================================="

# 千位分隔符
# puts 1234.5678.to_s(:rounded, precision: 2) # "1234.57"

# 货币格式
# puts 1234.5.to_s(:currency)          # "$1,234.50"

puts "==========================================="

name = "Alice"
age = 25
puts "My name is #{name} and I'm #{age} years old"
# 输出：My name is Alice and I'm 25 years old

puts "==========================================="

# 去除缩进 (Ruby 2.3+)
str = <<~SQL
  SELECT *
  FROM users
  WHERE id = 1
SQL

puts str.strip
# 输出：
# SELECT *
# FROM users
# WHERE id = 1

puts "==========================================="

# 提取数字
phone = "Tel: 123-456-7890"
puts phone.scan(/\d+/).join   # "1234567890"

# 邮箱验证
email = "test@example.com"
puts email.match?(/\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i)
# 输出：true
puts "==========================================="